package com.chen.qmzy.tool;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    //业务码，比如成功、失败、权限不足等 code，可自行定义
    @ApiModelProperty(value = "返回码",position = 1)
    private int code;
    //返回信息，后端在进行业务处理后返回给前端一个提示信息，可自行定义
    @ApiModelProperty(value = "返回信息",position = 2)
    private String message;
    //数据结果，泛型，可以是列表、单个对象、数字、布尔值等
    @ApiModelProperty(value = "返回数据",position = 3)
    private T data;

    public Result() {
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }


    private static final String DEFAULT_OK_MESSAGE = "成功";
    private static final String DEFAULT_ERROR_MESSAGE = "失败";
    private static final int RESULT_OK = 200;
    private static final int RESULT_ERROR = 500;

    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.setCode(RESULT_OK);
        result.setMessage(DEFAULT_OK_MESSAGE);
        return result;
    }

    public static <T> Result<T> ok(String message) {
        Result<T> result = new Result<>();
        result.setCode(RESULT_OK);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setCode(RESULT_OK);
        result.setMessage(DEFAULT_OK_MESSAGE);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(RESULT_ERROR);
        result.setMessage(DEFAULT_ERROR_MESSAGE);
        return result;
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(RESULT_ERROR);
        if (StrUtil.isBlank(message)) {
            result.setMessage(DEFAULT_ERROR_MESSAGE);
        } else {
            result.setMessage(message);
        }
        return result;
    }

    public static <T> Result<T> error(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }


}
